package com.jiaai.cloud.gift.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.CustomPage;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.gift.entity.TLiveGift;
import com.jiaai.cloud.gift.entity.TLiveGiftSearch;
import com.jiaai.cloud.gift.service.ITLiveGiftService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

/**
 * <p>
 * 直播间礼物设置 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-01
 */
@RestController
@RequestMapping("/api/manage/gift")
public class TLiveGiftController {
    @Autowired
    private ITLiveGiftService itLiveGiftService;


    //添加
    @RequestMapping( value = "/addGift" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})

    public  Object addRole( TLiveGift tLiveGift) throws ServiceException{
        try {
            tLiveGift.setTime(new Date());
            return RestResultGenerator.genSuccessResult(itLiveGiftService.insertOrUpdate(tLiveGift));
        } catch (Exception e) {
            throw new ServiceException("添加失敗", e.getMessage());
        }
    }

    //可以根据Id查找,
    @RequestMapping(value = "/findByAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll(TLiveGiftSearch search) throws ServiceException {
        try {
            Page page=search.getPagePlus();
            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getName())){
                wrapper.like("g.name",search.getName());
            }
            return RestResultGenerator.genSuccessResult(itLiveGiftService.findGiftFully(page,wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //可以根据Id查找,
    @RequestMapping(value = "/find", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll(FrontPage page) throws ServiceException {
        try {
            EntityWrapper wrapper=new EntityWrapper();
            return RestResultGenerator.genSuccessResult( itLiveGiftService.findGiftFully(page.getPagePlus(),wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    // 通过Id删除
    @RequestMapping(value = "/deleteId/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("id")  Integer id) throws ServiceException {
        try {
            itLiveGiftService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    @RequestMapping(value = "/giftCount" ,method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object selectList() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itLiveGiftService.selectGiftCount());
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    @RequestMapping(value = "/giftMoneyCount" ,method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object giftMoneyCount() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itLiveGiftService.selectGiftMoneyCount());
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    @RequestMapping(value = "/giftDataCount" ,method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object giftDataCount() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itLiveGiftService.selectGiftDataCount());
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }
    //通过改anchorId改status状态
    @RequestMapping(value = "/changeStatus/{id}/{is_continuity}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object changeStatus( @PathVariable("id") Integer id, @PathVariable("is_continuity") Integer is_continuity ) throws ServiceException {
        try {
            TLiveGift tLiveGift = new TLiveGift();
            tLiveGift.setId(id);
            tLiveGift.setIs_continuity(is_continuity);
            itLiveGiftService.updateOne(tLiveGift);
            return RestResultGenerator.genSuccessResult("");
        }catch (Exception e) {
            throw new ServiceException("修改失敗", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object edit(@RequestBody TLiveGift tLiveGift)throws ServiceException{
        try {
            tLiveGift.setIcon("http:www.com");
            tLiveGift.setTime(new Date());
            itLiveGiftService.insertOrUpdate(tLiveGift);
            return null;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }

}
